#include "materialtypelist.h"
#include <QScroller>
#include "dataprovider.h"

MaterialTypeList::MaterialTypeList(QWidget* parent)
    : QListWidget(parent) {
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QScroller::grabGesture(this, QScroller::TouchGesture);
    setFixedWidth(64);
    setUniformItemSizes(true);
    applyStyles();

    auto dataProvider = DataProvider::instance();
    auto materialTypes = dataProvider->GetMaterialTypes();
    LoadItems(*materialTypes);
    setCurrentRow(0);

    setupConnections();
}

void MaterialTypeList::setupConnections(){
    connect(this, &QListWidget::itemClicked, this, &MaterialTypeList::onItemClicked);
}

void MaterialTypeList::applyStyles(){
    setStyleSheet(
        /* 选中状态样式 */
        R"(
        QListWidget {font: 14px; border: 0px}
        QListWidget::item { font: 10px; }
        QListWidget::item:selected {
            background: #2196F3;
            color: white;
            border-left: 4px solid #1976D2;
        }
        /* 悬停效果 */
        QListWidget::item:hover {
            background: #E3F2FD;
        })"
        );
}

void MaterialTypeList::LoadItems(const QStringList& items) {

    clear();
    foreach (const QString &item, items) {
        QListWidgetItem *listItem = new QListWidgetItem(item);
        listItem->setSizeHint(QSize(60,60));
        listItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        listItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        addItem(listItem);
    }
}

void MaterialTypeList::onItemClicked(QListWidgetItem *item) {
    setCurrentItem(item);
    auto dataProvider = DataProvider::instance();
    dataProvider->SetSelectedMaterialType(item->text());
}
